#ifndef __FILE_REMOVER_H__
  #define __FILE_REMOVER_H__

#include <string>

#include "date_time.h"
#include "file_system.h"

using namespace std;

//! This class helps to remove protocol files after a period of time

/*!
A helpful class which can be used in combination with a logger to remove log files, which
are older than a week, a month or a year.

This is necessary to avoid log file mess and return the tidiness to keep the log files organised by removing
the old log files.
*/
class file_remover {
public:
  enum interval { older_than_a_week, older_than_a_month, older_than_a_year };

  //! removes all files in the given path, only if (s1 <= file_name < s2)
  static void remove_files(const string &path, const string &s1, const string &s2) {
    file_system fs;

    vector<string> file_list;
    fs.get_files(path, file_list);

    const size_t count = file_list.size();
    for (size_t i = 0; i < count; ++i) {
      const string &str_tmp = file_list[i];

      if (str_tmp >= s1 && str_tmp < s2) {
        int result = 0;

        if (path.empty() == false)
          result = remove((path + '/' + str_tmp).c_str());
        else
          result = remove(str_tmp.c_str());

        if (result != 0)
          throw file_exception("Could not remove the file!");
      }
    }
  }

  //! removes all files in the given path, only if (s1 <= file_name < (s1 + date_interval))
  static void remove_protocol_files(const string &path, const string &s1, interval i) {
    date_time d1; // automatically set to the current date & time

    switch (i) {
      case older_than_a_week: {
        d1 = d1 - date_time(0, 0, 7);
        break;
      }

      case older_than_a_month: {
        d1 = d1 - date_time(0, 1);
        break;
      }

      case older_than_a_year: {
        d1 = d1 - date_time(1);
        break;
      }

      default: {
        throw parameter_exception("Undefined interval!");
      }
    }

    file_remover::remove_files(path, s1, s1 + d1.to_string(DATE_FORMAT) + ".txt");
  }
};

#endif
